/**
 * Copyright 2009 Takahiko Ito
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.unigram.oluolu.ngram;

import java.io.IOException;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import org.unigram.oluolu.common.FsUtil;
import org.unigram.oluolu.common.OluoluLogger;

/**
 * FrequerntNGramExtraction.
 */
public class FrequentNGramExtration extends
    Configured implements Tool {

    /** logger.  */
    private static OluoluLogger logger 
        = OluoluLogger.getLogger();    

    /**
     * Main method.
     * 
     * @param args arguments containing input and output files and
     *         parameters.
     * @throws Exception -
     */
    public static void main(final String[] args)
        throws Exception {
        int exitCode = ToolRunner.run(new FrequentNGramExtration(), args);
        System.exit(exitCode);
    }

    /**
     * Extract frequent Ngram.  
     * 
     * @param input input file path
     * @param outputPrefix prefix of output file
     * @param conf Configuration instance 
     * @return return 0 when succeeded otherwise return 1 
     * @throws IOException -
     * @throws InterruptedException -
     * @throws ClassNotFoundException -
     */
    public final int runNGram(final String input, 
            final String outputPrefix, final Configuration conf)
    throws IOException, InterruptedException,
            ClassNotFoundException {

        Path inputPath = new Path(input);
        Path outputPath = new Path(outputPrefix + "."
                + conf.get(NgramConstants.NGRAM_SUFFIX,
                        NgramConstants.DEFAULT_NGRAM_SUFFIX));

        FsUtil.checkPath(outputPath, conf);

        Job job = new Job(conf);
        job.setJarByClass(FrequentNGramExtration.class);
        FileInputFormat.addInputPath(job, inputPath);
        FileOutputFormat.setOutputPath(job, outputPath);
        job.setMapperClass(FrequentNGramMapper.class);
        job.setReducerClass(FrequentNGramReducer.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
        return job.waitForCompletion(true) ? 0 : 1;
    }

    /**
     * Run configuration settings from parameters and then runNGram.
     * @param args arguments
     * @throws IOException -
     * @throws InterruptedException -
     * @throws ClassNotFoundException -
     * @return int
     */
    public final int run(final String[] args)
        throws IOException, InterruptedException,
            ClassNotFoundException {

        Configuration.addDefaultResource("conf/oluolu-default.xml");
        Configuration.addDefaultResource("conf/oluolu-site.xml");
        Configuration conf = getConf();

        int minGram = 1;
        int maxGram = 2;
        int minimumSupport = 1;
        String input = "";
        String outputPrefix = "";

        for (int i = 0; i < args.length; ++i) {
            if ("-input".equals(args[i])) {
                input = args[++i];
            } else if ("-output".equals(args[i])) {
                outputPrefix = args[++i];
            } else if ("-minimumSupport".equals(args[i])) {
                minimumSupport = Integer.parseInt(args[++i]);
                conf.setLong(NgramConstants.NGRAM_MINIMUM_SUPPORT,
                        minimumSupport);
            } else if ("-minimumGram".equals(args[i])) {
                minGram = Integer.parseInt(args[++i]);
                conf.setLong(NgramConstants.NGRAM_MINIMUM_GRAM, minGram);
            } else if ("-maximumGram".equals(args[i])) {
                minGram = Integer.parseInt(args[++i]);
                conf.setLong(NgramConstants.NGRAM_MAXIMUM_GRAM, maxGram);
            } else if ("-help".equals(args[i])) {
                this.showParameters();
                return 0;
            }
        }
                

        if (input.length() == 0) {
            logger.logSevere("Invalid Input");
            return 1;
        } else if (outputPrefix.length() == 0) {
            logger.logSevere("Invalid Output Prefix");
            return 1;
        }
        return this.runNGram(input, outputPrefix, conf);
    }
    
    /**
     * Show parameters for FreqentNGramExtraction.
     */
    private void showParameters() {
        System.out.println("Common paramters:");
        System.out.println("    -input INPUT                " 
                + "use INPUT as input resource");
        System.out.println("    -output OUTPUT              " 
                + "use OUTPUT as outupt prefix");
        System.out.println("    [-minimumSupport MIN]       "
                + "use MIN as the minimum frequency "
                + "of the query string");        
        System.out.println("    [-minimumGram MIN]           "
                + "use MIN as the minimum number of grams");
        System.out.println("    [-maximumGram MAX]           "
                + "use MAX as the maximum number of grams");                
        System.out.println("    [-help]                      "
                + "show usage");
    }
        
}


